﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CSharpDay06__MVC_with_User_Input_.Models;

//Classes must be in the same namespace in order to communicate with each other OR say "using" the respective namespace to access its classes.
namespace CSharpDay06__MVC_with_User_Input_.Controllers
{
    //Controller is a pre-defined template that sends information to HomeController. Similiar to a prototype. Provides all the methods needed for MVC to the Controller that WE Created aka HomeController
    public class HomeController : Controller
    {
        //Result of an action method. Usually taking you to a specific page on the website (Index, About, Contacts, etc)
        public ActionResult Index()
        {    //Return view is what is going to be returned after WHATEVER action is taken.
            // DB.Shapes is a STATIC list of all shapes. Defined in DB Class page.
            return View(DB.Shapes);
        }

        // Home/About/1- url: "{controller}/{action}/{id}"  this is passed in by the URL. "1" is the overloaded action result's ID.
        //int id is passed into this ActionResult in order to alert the program to the fact that we want to see a specific item from the list of Shapes.  
        public ActionResult About(int id)
        {
            //A new object from the list Shape that has a specific id. The first Id that matches will be returned. REMEMBER the ID is ORIGINALLY generated within the Shapes List found on the DB page. 

            //It is important to remember that because the ids are generated by the list they can be navigated to by adding the actual id number to the END of the URL for the related ActionResult field.
            Shape model = DB.Shapes.FirstOrDefault(x => x.Id == id);
            //Here we pass the model object taken from the list Shape into the return View so that it may be displayed. 
            return View(model);
        }
        //Nothing to see here. Move along.
        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        //Here we delete an object from the Shapes List as well as removing its id from the website. This is attatched to a button in the HTML
        public ActionResult Delete(int id)
        {
            //Here we define and locate the actual object we want to delete by using its ID.
            Shape model = DB.Shapes.FirstOrDefault(x => x.Id == id);
            //here we remove the defined object by going to the Database, the list and invoking the remove action.
            DB.Shapes.Remove(model);
            //This returns to the index page via magically putting index inside string commas.
            return RedirectToAction("Index");
        }
        //what do I do???
        public ActionResult AddTriangle()
        {
            return View(new Triangle());
        }
        [HttpPost]
        public ActionResult AddTriangle(int Base, int Height)
        {
            Triangle triforce = new Triangle();
            triforce.Height= Height;
            triforce.Base = Base;
            triforce.Id = DB.Shapes.LastOrDefault().Id;
            triforce.Id += 1;
            DB.Shapes.Add(triforce);

            return RedirectToAction("Index");
        }
    }
}